<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('渠道代码-新增')" />
	<th:block th:include="include :: businessCSS" />
	<style>
		.preservationBtn{padding: 10px;text-align: center;}
		.preservationBtn input{font-size: 16px;background-color: rgb(80, 138, 238);color: #fff;border: none;padding: 5px 30px;border-radius: 10px;cursor: pointer;}
	</style>

</head>

<body class="no-skin">

<div class="main-container" id="main-container">
	<div class="main-content">
		<form class="layui-form head-form">
			<div class="north" style="background-color: #fff;">
				<div class="forminput"></div>
				<!-- 明细特殊模板自生成 -->
				<div>
					<table class="layui-hide" id="zjdhtable" lay-filter="zjtable"></table>
					<script type="text/html" id="jptoolbar">
						<div style="padding: 16px;display: flex;justify-content: space-between;">
							<div style="font-weight: 700;font-size: 18px;">计重梯度单价</div>
							<div>
								<button class="layui-btn layui-btn-normal dhicon" th:classappend="${enableEdit} == 1 ?'':'layui-btn-disabled'" type="button" th:disabled="${enableEdit} == 1 ?false:true" lay-event="addbtn" id="btn-sh">新增</button>
								<button class="layui-btn layui-btn-danger dhicon" th:classappend="${enableEdit} == 1 ?'':'layui-btn-disabled'" type="button" th:disabled="${enableEdit} == 1 ?false:true" lay-event="mjbtn_save" id="btn-sh">删除</button>
							</div>
						</div>
					</script>
				</div>
				<div class="layui-form-item preservationBtn">
					<input type="button" lay-submit lay-filter="btn-add" id="btn-add" value="保存">
				</div>
			</div>
		</form>
	</div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: businessJS" />
<th:block th:include="include :: inlineJS" />

<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "storage/tsChannelcode";
	var	k_fields=[]
	var fields = [[${fields}]];
	var fieldsList = [[${fieldsList}]];
	var fieldsHiddenList = [[${fieldsHiddenList}]];
	var all_fields= fieldsList;
	var $,layer,form,table;
	var enableEdit = [[${enableEdit}]];
	var gridColumnsZJ = [[${gridColumnsZJ}]];
	var listData1 = [[${listData1}]];
	var listData2 = [];
	layui.use(['laydate','form','layer','jquery','util','element','upload','table'], function () {
		$ = layui.$,layer = layui.layer,form = layui.form,laydate = layui.laydate,util = layui.util,element=layui.element,upload = layui.upload;
		headcom3()

		form.on('submit(btn-add)', function(data){
			var is_return = false//是否中断保存
			fieldsList.map((v,i)=>{
				if(v.fieldFrontRequired){
					if(!$('#'+v.fields).val()){
						layer.msg(v.fieldName+'不能为空')
						is_return=true
					}
				}
			})
			if(is_return) return;
			$.modal.loading("正在处理中，请稍后...");
			$.post("/storage/tsChannelcode/addpost",{infoMap:JSON.stringify(data.field),grads:JSON.stringify(listData2)},function(res){
				$.modal.closeLoading()
				layer.msg(res.msg);
				if (res.code == 0){
					setTimeout(function(){
						window.location.href = '/storage/tsChannelcode/edit?guid=' + res.data+'&type=1';
					}, 1000);
				}
			});
			return false;
		});

		$.tablelist.init({
			elem: '#zjdhtable'
			, data: listData2
			, page: false
			, even: true
			, limit: 10000
			, totalRow: gridColumnsZJ.totalRow
			, cols: [gridColumnsZJ.gridBody]
			, toolbar: '#jptoolbar'
			, height: '300'
			, done: function (res, curr, count) {
				listData2 = table.cache.zjdhtable;//每次重载都赋值更新表缓存
			}
		});


		let a = 0;
		//头工具栏事件
		table.on('toolbar(zjtable)', function (obj) {
			switch (obj.event) {
				case 'addbtn':
					gridColumnsZJ.gridNewLine.guid = a++;
					listData2.push(gridColumnsZJ.gridNewLine);
					table.reload('zjdhtable', {data :listData2});
					break;
				case 'mjbtn_save':
					var objdata=table.checkStatus('zjdhtable')
					objdata=objdata.data
					listData2= table.cache.zjdhtable;
					if(objdata.length>0){
						objdata.forEach(item => {
							listData2 = listData2.filter(items => items.guid!= item.guid);
						});
						table.reload('zjdhtable', {data:listData2});
					}else{
						layer.msg("请选择数据");
					}
					break;
			};
		});

		//监听单元格编辑
		table.on('edit(zjtable)', function(obj) {
			let fxa = true;
			let name;
			//判断是否数值匹配
			listData2.map((n,m)=> {
				if (obj.field == 'startSection') {
					name = "开始值";
				}
				if (obj.field == 'endSection') {
					name = "结束值";
				}
				if (obj.field == 'kg') {
					name = "数值(KG)";
				}
				if(n.guid == obj.data.guid){
					if (!$.isNumeric(obj.value)){
						n[obj.field] = "";
						layer.msg(name + "只允许输入数字,请核实后在操作!")
						fxa = false;
						return
					}
				}

			})
			if (!fxa) {
				table.reload('zjdhtable', {data: listData2});
			}

		})



	});

</script>

</body>
</html>